package com.xlf.service.judge.mq.consumer;



import com.rabbitmq.client.Channel;
import com.xlf.common.redis.service.RedisCache;
import com.xlf.pojo.dto.TestJudgeReq;
import com.xlf.service.judge.config.JudgeConfig;
import com.xlf.service.judge.service.JudgeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.SerializationUtils;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Map;

import static com.xlf.common.core.constant.MqConstant.Judge.*;


@Service
@Slf4j
public class JudgeConsumer {

    @Autowired
    JudgeConfig judgeConfig;

    @Autowired
    JudgeService judgeService;

    @Autowired
    RedisCache redisCache;

    /**
     *
     */
    @RabbitListener(queues = {JUDGE_PROBLEM_QUEUE},containerFactory = "customContainerFactory")
    private void JudgeProblemConsumer(String msg,Message message, Channel channel) {
        String judgeId = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("判题队列接收到了：" + judgeId);
        try {
            judgeService.judge(judgeId);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("判题队列判题出问题:" + e.getMessage());
//            e.printStackTrace();
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     *
     */
    @RabbitListener(queues = {JUDGE_TEST_QUEUE},containerFactory = "customContainerFactory")
    private void JudgeTestConsumer(String msg,Message message, Channel channel) {
        byte[] body = message.getBody();
        TestJudgeReq testJudgeReq = (TestJudgeReq) SerializationUtils.deserialize(body);
        log.info("Test队列接收到了：" + testJudgeReq);
        try {
            judgeService.testJudge(testJudgeReq);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error("Test队列判题出问题:" + e.getMessage());
            try {
                channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException ex) {
                log.error("Test队列拒绝失败:" + testJudgeReq.getUniqueKey());
            }
        }

    }


    /**
     *
     */
    @RabbitListener(queues = {JUDGE_TEST_DIE_QUEUE})
    private void JudgeTestDieConsumer(String msg,Message message, Channel channel) {
        byte[] body = message.getBody();
        TestJudgeReq testJudgeReq = (TestJudgeReq) SerializationUtils.deserialize(body);
        log.error("用例测试死信队列接收到了：" + testJudgeReq.toString());
        MessageProperties properties = message.getMessageProperties();
        Map<String, Object> headers = properties.getHeaders();
        // 获取消息进入死信队列的原因
        String deathExchange = (String) headers.get("x-first-death-exchange");
        String deathReason = (String) headers.get("x-first-death-reason");
        log.error(testJudgeReq.getUniqueKey() + ":进入死信队列的原因：" + deathReason + "-------来源交换机：" +  deathExchange);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
//            e.printStackTrace();
            log.error("用例测试死信队列拒绝失败:" + testJudgeReq.getUniqueKey());
        }
    }

    /**
     *
     */
    @RabbitListener(queues = {JUDGE_PROBLEM_DIE_QUEUE})
    private void JudgeProblemDieConsumer(String msg,Message message, Channel channel) {
        String judgeId = new String(message.getBody(), StandardCharsets.UTF_8);
        log.error("判题死信队列接收到了：" + judgeId);
        MessageProperties properties = message.getMessageProperties();
        Map<String, Object> headers = properties.getHeaders();
        // 获取消息进入死信队列的原因
        String deathExchange = (String) headers.get("x-first-death-exchange");
        String deathReason = (String) headers.get("x-first-death-reason");
        log.error(judgeId + "进入死信队列的原因：" + deathReason + "-------来源交换机：" +  deathExchange);

        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (IOException e) {
//            e.printStackTrace();
            log.error("判题死信队列拒绝失败:" + judgeId);

        }
    }

}
